import java.util.ArrayList;


public class Test {

	public static void main(String[] args) {
		
		String filename = "machines.txt";
		ArrayList<String> data = (new FileRead(filename)).getLines();
		ArrayList<MachineData> mData = new ArrayList<MachineData>();
		
		for(String s: data) {
			mData.add(new MachineData(s));
		}
		
		System.out.println(getMachine(mData.get(1), mData, 3));
		
		
	}
	
	public static int getMachine(MachineData m, ArrayList<MachineData> mData, int r) {
		
		int index = -1;
		
		for(int i = 0; i < mData.size(); i++) {
			MachineData d = mData.get(i);
			
			if(d.equals(m)) {
				index = i ;
			}
		}
		
		for(int i = 1; i <= Global.k; i++) {
			int temp = index - i;
			
			if(temp < 0) {
				temp = mData.size() + temp;
				if(temp != r) {
					return temp;
				}
			}
			else {
				if( temp != r) {
					return temp;
				}
			}
		}
		
		return -1;
	}
}
